/**
 * 删除上传的图片
 */
function deleteCover(value) {
    $("#" + value).remove();
}

/**
 * 会员
 */

define([
    'jquery',
    'baseUtils',
    'base',
    'msgTool',
    'css!global/plugins/wangEditor/wangEditor.min.css',
    'plugins/wangEditor/wangEditor.min',
    'css!global/plugins/fileInput/fileinput.min.css',
    'plugins/fileInput/fileinput.min',
    'plugins/bootstrap-table/bootstrap-table',
    "plugins/bootstrap-dialog/bootstrap-dialog",
    "css!global/plugins/bootstrap-dialog/bootstrap-dialog.css",
    'iCheck',
    'css!global/plugins/iCheck/icheck.min.css'
], function ($, baseUtils, base, msgTool, _, E, _, _, _, BootstrapDialog,iCheck) {
    //默认加载基础库
    base();
    //执行
    return {
    	 info: function () {  
    		 
    		 $("input[name='sex']").iCheck({
 				checkboxClass: 'icheckbox_square-green',
 				radioClass: 'iradio_square-green',
 				increaseArea: '10%' // optional
 			});

             /**
              * 提交食药从业人员申请
              */

             $("#submitManager").on('click', function () {
            	 var reg_phone = /^((1[3,5,8][0-9])|(14[5,7])|(17[0,6,7,8]))\d{8}$/;//验证手机号
        		 var reg_IDcard = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/; //验证身份证号
              	 var name = $("#name").val();
           	     var nation=$("#nation").val();
           	     var householdAddress=$("#householdAddress").val();
              	 var cardNumber= $("#cardNumber").val();
              	 var phone= $("#phone").val();
               if(name==''){
              	$("#name-error").html("(姓名不能为空)");
              	return false;
               }else{
            	$("#name-error").html("");   
               }
               if(nation==''){
           	   $("#nation-error").html("(民族不能为空)");
           	   return false;
               }else{
               $("#nation-error").html("");  
               }
               if(householdAddress==''){
                $("#householdAddress-error").html("(户籍地址不能为空)");
                return false;
               }else{
            	$("#householdAddress-error").html("");  
               }
               if(cardNumber==''){
              	 $("#cardNumber-error").html("(身份证号码不能为空)"); 
              	 return false;
               }else{
            	   if(cardType==1){
               		if (!reg_IDcard.test(cardNumber)){
                    		  $("#cardNumber-error").html("请输入正确的证件号码");
           					return false;
           				}else{
           				  $("#cardNumber-error").html("");	
           				}	
               	}else{
               		 $("#cardNumber-error").html("");	
               	}        
               }
               if(phone==''){
              	 $("#phone-error").html("(手机号不能为空)"); 
              	 return false;
               }else{
            	   if (!reg_phone.test(phone)){
                		  $("#phone-error").html("(请输入正确的手机号码)");
       					return false;
       				}else{
       				  $("#phone-error").html("");	
       				}
               }
               $("#saveManagerForm").submit();
              });
             
             $("#cancelManager").on('click', function () {
             	var applyId=$("#applyId").val();
             	var changeId=$("#changeId").val();
             	if(changeId==undefined){
             		window.location.href="/fooddrugApply/managerList?applyId="+applyId;
             	}
             	if(applyId==undefined){
             		window.location.href="/fooddrugChange/managerList?changeId="+changeId;
             	}
             });
             
      
             
           
         },

    	 /**
         * 列表
         */
        list: function () {

            var myTable = $('#managerList');
            var tableRefresh = function () {
                myTable.bootstrapTable('refresh', {url: 'member/api.json'})
            };

            //table当前行的响应事件
            window.operateEvents = {
                'click.update': function (e, value, row) {
                    winModal("编辑", row);
                },
                'click.delete': function (e, value, row) {
                    $.baseUtils.oneDelete({
                        row: row,
                        tableName: myTable,
                        nameText: '姓名',
                        nameValue: row.name,
                        url: '/fooddrug/deleteEmployee/action'
                    });
                }
            };

            //table表格
            myTable.bootstrapTable({
                url: 'member/api.json', // 接口 URL 地址
                cache: false, // 不缓存
                striped: true, // 隔行加亮
                pagination: true, // 开启分页功能
                sidePagination: 'server',//服务端分页
                sortOrder: 'desc', // 设置排序为反序 desc
                fixedColumns: true,//自动修复
                queryParams: queryParams,//搜索附加参数
                smartDisplay: false, // 智能显示 pagination 和 cardview 等
                toolbar: ".toolBar", //顶部工具菜单
                toolbarAlign: "right", //顶部工具位置
                mobileResponsive: "true", //手机模式自适应宽度（自动变成卡片模式）
                columns: [
                    {
                    	  field: 'rowno',
                          title: '序号',
                          formatter: function (value, row, index) {  
                              return index+1;  
                          },  
                          align:'center'
                    }, {
                        field: 'name',
                        title: '姓名',
                        align: 'center'
                    }, {
                        field: 'sex',
                        title: '性别',
                        formatter: function (value) {
                        	  var result = null;
                              switch (value) {
                                  case 1:
                                      result = "男";
                                      break;
                                  case 2:
                                      result = "女";
                                      break;
                                  default:
                                      result = "未知";
                                      break;
                              }
                              return result;
                        }
                    }, {
                        field: 'nation',
                        title: '民族',
                        formatter: function (value) {
                        	var result = null;
                        	switch(value)
                        	{
                        	case  1:result="汉族";  break;
                        	case  2:result="阿昌族";  break;
                        	case  3:result="白族";  break;
                        	case  4:result="保安族";  break;
                        	case  5:result="布朗族";  break;
                        	case  6:result="布依族";  break;
                        	case  7:result="朝鲜族";  break;
                        	case  8:result="达斡尔族";  break;
                        	case  9:result="傣族";  break;
                        	case  10:result="德昂族";  break;
                        	case  11:result="侗族";  break;
                        	case  12:result="东乡族";  break;
                        	case  13:result="独龙族";  break;
                        	case  14:result="鄂伦春族";  break;
                        	case  15:result="俄罗斯族";  break;
                        	case  16:result="鄂温克族";  break;
                        	case  17:result="高山族";  break;
                        	case  18:result="仡佬族";  break;
                        	case  19:result="哈尼族";  break;
                        	case  20:result="哈萨克族";  break;
                        	case  21:result="赫哲族";  break;
                        	case  22:result="回族";  break;
                        	case  23:result="基诺族";  break;
                        	case  24:result="京族";  break;
                        	case  25:result="景颇族";  break;
                        	case  26:result="柯尔克孜族";  break;
                        	case  27:result="拉祜族";  break;
                        	case  28:result="黎族";  break;
                        	case  29:result="傈僳族";  break;
                        	case  30:result="珞巴族";  break;
                        	case  31:result="满族";  break;
                        	case  32:result="毛南族";  break;
                        	case  33:result="门巴族";  break;
                        	case  34:result="蒙古族";  break;
                        	case  35:result="苗族";  break;
                        	case  36:result="仫佬族";  break;
                        	case  37:result="纳西族";  break;
                        	case  38:result="怒族";  break;
                        	case  39:result="普米族";  break;
                        	case  40:result="羌族";  break;
                        	case  41:result="撒拉族";  break;
                        	case  42:result="畲族";  break;
                        	case  43:result="水族";  break;
                        	case  44:result="塔吉克族";  break;
                        	case  45:result="塔塔尔族";  break;
                        	case  46:result="土族";  break;
                        	case  47:result="土家族";  break;
                        	case  48:result="佤族";  break;
                        	case  49:result="维吾尔族";  break;
                        	case  50:result="乌兹别克族";  break;
                        	case  51:result="锡伯族";  break;
                        	case  52:result="瑶族";  break;
                        	case  53:result="彝族";  break;
                        	case  54:result="裕固族";  break;
                        	case  55:result="藏族";  break;
                        	case  56:result="壮族";  break;
                        	default:
                        	  result=="未知";  break;
                        	}
                        	 return result;
                        }
                    },
                    {
                        field: 'job',
                        title: '职务',
                        formatter: function (value) {
                            var result = null;
                            switch (value) {
                                case 1:
                                    result = "食品安全技术人员";
                                    break;
                                case 2:
                                    result = "食品安全管理人员";
                                    break;
                                case 3:
                                    result = "其他人员";
                                    break;
                                default:
                                    result = "未知";
                                    break;
                            }
                            return result;
                        }
                    }, {
                        field: 'cardNumber',
                        title: '身份证号码',
                        align: 'center'
                    }, {
                        field: 'phone',
                        title: '手机号码',
                        align: 'center'
                    },  {
                        field: 'action',
                        title: '操作',
                        align: 'center',
                        formatter: operateFormatter,
                        events: operateEvents
                    }
                ]
            });
            //查询条件  
            function queryParams(params) {  
            	var applyId=$("#applyId").val();
            	var changeId=$("#changeId").val();
            	if(changeId==undefined){
            		return {  
            			job:"2",
            			applyId:applyId
            		};  
            	}
            	if(applyId==undefined){
            		return {  
            			job:"2",
            			changeId:changeId
            		};  
            	}
            }  

        },

        /**
         * 查看
         */
        view: function () {

        },

        /**
         *编辑保存
         */
        save: function () {
            /**
             * 富文本编辑器
             */
            var editor = new E('#myEditer');
            var deails = $("textarea[name='intro']");
            // 监控变化，同步更新到 textarea
            editor.customConfig.onchange = function (html) {
                deails.text(html);
            };
            // 配置服务器端地址
            editor.customConfig.uploadImgServer = '/common/uploader/index';
            // 将图片大小限制为 30M
            editor.customConfig.uploadImgMaxSize = 30 * 1024 * 1024;
            // 限制一次最多上传 5 张图片
            editor.customConfig.uploadImgMaxLength = 50;
            // 上传文件名，类似于input>type=file>name
            editor.customConfig.uploadFileName = 'files';
            // 将 timeout 时间改为 20s
            editor.customConfig.uploadImgTimeout = 20000;
            // 自定义上传参数
            editor.customConfig.uploadImgParams = {
                filesType: 2
            };
            editor.create();

            /**
             * 文件上传
             */
            $("#fileUploader").fileinput({
                //设置语言
                language: 'zh',
                //上传的地址
                uploadUrl: '/common/uploader/index',
                method: 'post',
                msgPlaceholder: '请选择图片，最多可上传10张。',
                //附加属性(0图片，1文件)
                uploadExtraData: {filesType: 1},
                //上传类型
                allowedFileExtensions: ['jpg', 'gif', 'png', 'jpeg', '7z', 'rar', 'zip', 'doc', 'docx', 'xlsx', 'xls', 'pdf'],
                //是否显示图片展示框
                showPreview: false,
                //是否显示上传按钮
                showUpload: true,
                showCancel: false,
                //是否显示上传输入框
                showCaption: true,
                //是否显示删除键
                showRemove: true,
                //浏览图标
                browseIcon: '<i class="iconfont icon-wenjianjia"></i>&nbsp;',
                //浏览样式
                browseClass: 'btn btn-success btn-outline',
                //上传图标
                uploadIcon: '<i class="iconfont icon-upload"></i>&nbsp;',
                //上传样式
                uploadClass: 'btn btn-success btn-outline',
                //移除图标
                removeIcon: '<i class="iconfont icon-remove"></i>&nbsp;',
                //移除样式
                removeClass: 'btn btn-info btn-outline',
                //单位为kb，(30M)如果为0表示不限制文件大小
                maxFileSize: 30720,
                //错误显示位置
                elErrorContainer: "#file-upload-error",
                //错误显示位置节点样式
                msgErrorClass: 'alert alert-block alert-danger',
                //验证数量
                validateInitialCount: true
            }).on("fileuploaded", function (event, data) {
                $("#notAttr").remove();
                if (data.response !== null && data.response.status) {
                    $("#file-upload-success>table>tbody").append("<tr id=\"" + data.response.nodeId + "\"><td><input type='hidden' name='attachList' value=\"" + data.response.url + "##" + data.response.size + "\"><a target=\"_blank\" href=\"" + data.response.domain + data.response.url + "\">" + data.response.name + "</a></td><td>" + data.response.size + "</td><td><button type='button' class=\"btn btn-xs btn-danger deletetCover\" onclick=\"deleteCover('" + data.response.nodeId + "')\">删除</button></td></tr>");
                } else {
                    $("#file-upload-error").text(data.response.message);
                }
            });
          
                       
            $("#saveProjectForm").validate({
                rules: {
                 ignore:"",
                 cateList:{
                		 required: true
                	},
              	  title: {
                        required: true
                    },
                    amount: {
                        required: true,
                        digits: true
                    },
                    duration: {
                        required: true,
                        digits: true
                    },
                    peopleNumber: {
                        required: true,
                        digits: true
                    },
                    workAddress: {
                        required: true,
                        rangelength: [2, 100]
                    },
                    beginTime: {
                        required: true,
                        date: true
                    },
                    description: {
                        required: true
                    },
                    intro:{
                  	  required: true,
                  	 rangelength: [10, 100]
                    }
                },
                messages: {
                  cateList:{
                		 required: errorIcon +"项目分类不能为空"
                 },
              	  title: {
                        required: errorIcon + "标题不能为空"
                    },
                    amount: {
                        required: errorIcon + "预算金额不能为空",
                        digits: $.validator.format(errorIcon + "只能输入数字。")
                    },
                    duration:{
                  	  required: errorIcon + "预算工期不能为空",
                  	  digits: $.validator.format(errorIcon + "只能输入数字。")
                    },
                    peopleNumber: {
                  	  required: errorIcon + "不能为空",
                  	  digits: $.validator.format(errorIcon + "只能输入数字。")
                    },
                    workAddress: {
                        required: errorIcon + "详细地址不能为空",
                        rangelength: $.validator.format(errorIcon + "字数在2-100个之间。")
                    },
                    beginTime: {
                        required: errorIcon + "开始时间不能为空",
                        birthday: $.validator.format(errorIcon + "格式错误，范例：2017-08-10")
                    },
                    description: {
                        required: errorIcon + "不能为空"
                    },
                    intro: {
                        required: errorIcon + "不能为空"
                    }
                }
            });




        }
    }
});


